package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;
    @Override
    public void add(Permission permission) {

        log.debug("service:{"+permission+"}");
        permissionDao.add(permission);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("service:{"+queryPageBean+"}");
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询数据
        Page<Permission> page= permissionDao.findByCondition(queryPageBean.getQueryString());
        //封装到PageResult
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void delById(Integer id) {
        //是否被role关联
      long count =  permissionDao.findCountById(id);
      //说明被关联
      if(count>0){
          throw new RuntimeException("被role关联 不能删除");
      }else {
          permissionDao.delById(id);
      }
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void edit(Permission permission) {
         permissionDao.edit(permission);
    }
}
